<template>
  <div class="app-container">
    <div class="filter-container">
      <v-search :isSearchKeywords="false" :selecteList="importanceOptions" @onSearch="searchData"></v-search>
    </div>
    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;">
      <el-table-column label="序号" prop="id" sortable="custom" align="center" width="210">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="分组管理员" align="center"  width="210">
        <template slot-scope="scope">
          <span>{{ scope.row.group_name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" width="210">
        <template slot-scope="scope">
          <span>{{ scope.row.nickname }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作内容" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.action }}</span>
        </template>
      </el-table-column>
      <el-table-column label="时间" width="250">
        <template slot-scope="scope">
         <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
  </div>
</template>

<script>
import { getLog, getUsers } from '@/api/group'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import vSearch from '@/components/Search'

export default {
  components: {
    Pagination,
    vSearch
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: false,
      listQuery: {
        page: 1,
        limit: 10,
        sort: '-id',
        where: []
      },
      downloadLoading: false,
      importanceOptions: []
    }
  },
  created() {
    this.getList()
    this.getUsers()
  },
  methods: {
    getList() {
      this.listLoading = true
      getLog(this.listQuery).then(response => {
        this.list = response.data.data
        this.total = response.data.total
        this.listLoading = false
      })
    },
    getUsers() {
      this.listLoading = true
      getUsers({
        limit: 50
      }).then(response => {
        const list = response.data.data
        list.map(res => {
          this.importanceOptions.push({
            id: res.id,
            name: res.admin_name
          })
        })
      })
    },
    searchData(data) {
      this.listQuery.page = 1
      this.listQuery.where = []
      if (data.selected) {
        this.listQuery.where.push({
          field: 'admin_log.admin_uid',
          op: 'eq',
          value: data.selected
        })
      }
      if (data.dateStart) {
        this.listQuery.where.push({
          field: 'admin_log.action_time',
          op: 'between',
          value: [data.dateStart, data.dateEnd]
        })
      }
      this.getList()
    }
  }
}
</script>
